import Vue from "vue"
import Swiper from "swiper"


Vue.directive("swiper", {
    inserted(el, binding, vnode) {

        // console.log(vnode.context.datalist.length)  //通过vnode 拿到 length  3 

        // 最后一个元素插入完毕了,再进行一次实例化操作就可以了
        if (binding.value.current === binding.value.length - 1) {
            //可以通过  binding.arg    v-swiper:swiper => swipeer 得到轮播图类名
            //可以通过  binding.value.swiper     得到轮播图类名
            new Swiper("." + binding.value.swipe, {//默认swiper会执行3次,因为插入了3个swiper-slide的真实dom

                // slidesPerView: 4,
                // spaceBetween: 10,
                // freeMode: true,
                ...binding.value.free,
                pagination: {
                    el: ".swiper-pagination"
                }
            })
        }

    }
})


Vue.directive("hometitle", {

    inserted(el, binding) {

            window.onscroll = function () {
                if ((document.body.scrollTop || document.documentElement.scrollTop) === binding.value.num) {
                    el.style.background = ""
                    el.style.color = "#fff"
                } else {
                    el.style.background = "#fff"
                    el.style.color = "#000"
                }
            }
    },
    unbind() {
        window.onscroll = null
    }
})